﻿using System;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class CpSjCx : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = BCpSjCx.GetCpSjDjr();
                if (ds != null)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DownListDjr.Items.Add(ds.Tables[0].Rows[i]["登记人"].ToString());
                    }
                }
                BindData();
            }
        }

        protected void Pager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            string sCpSjLb = string.Empty;
            if (Request.QueryString["CpSjLb"] != null)
            {
                sCpSjLb = Request.QueryString["CpSjLb"].Trim();
            }
            int iCpSjZt = -1;
            if ((Request.QueryString["CpSjZt"] != null) && BGlobal.IsNumeric(Request.QueryString["CpSjZt"].Trim()))
            {
                iCpSjZt = Convert.ToInt32(Request.QueryString["CpSjZt"].Trim());
            }
            string sCpSjDjr = string.Empty;
            if (Request.QueryString["CpSjDjr"] != null)
            {
                sCpSjDjr = Request.QueryString["CpSjDjr"].Trim();
            }
            string sCpSjWhere = string.Empty;
            if (Request.QueryString["CpSjWhere"] != null)
            {
                sCpSjWhere = Request.QueryString["CpSjWhere"].Trim();
            }
            int iCpSjIdYy = -1;
            if (sCpSjWhere.IndexOf("FindReferences|") == 0 && BGlobal.IsNumeric(sCpSjWhere.Substring(15))) // 查找引用
            {
                iCpSjIdYy = Convert.ToInt32(sCpSjWhere.Substring(15));
                sCpSjWhere = string.Empty;
            }
            int iWgjIdYy = -1;
            if (sCpSjWhere.IndexOf("FindWgjReferences|") == 0 && BGlobal.IsNumeric(sCpSjWhere.Substring(18))) // 查找引用
            {
                iWgjIdYy = Convert.ToInt32(sCpSjWhere.Substring(18));
                sCpSjWhere = string.Empty;
            }
            DataSet ds = null;
            if (sCpSjLb != string.Empty || iCpSjZt != -1 || sCpSjDjr != string.Empty || sCpSjWhere != string.Empty || iCpSjIdYy != -1 || iWgjIdYy != -1)
            {
                if (!Page.IsPostBack)
                {
                    DataSet dsCount = BCpSjCx.GetCpSjCountByCx(sCpSjLb, iCpSjZt, sCpSjDjr, sCpSjWhere, iCpSjIdYy, iWgjIdYy);
                    if (dsCount != null)
                    {
                        Pager1.RecordCount = (int)dsCount.Tables[0].Rows[0][0];
                    }
                    if (Request.QueryString["PageIndex"] != null && BGlobal.IsNumeric(Request.QueryString["PageIndex"]))
                    {
                        Pager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["PageIndex"]);
                    }
                }
                ds = BCpSjCx.GetCpSjByCx(sCpSjLb, iCpSjZt, sCpSjDjr, sCpSjWhere, iCpSjIdYy, iWgjIdYy, Pager1.PageSize, Pager1.CurrentPageIndex);
            }
            if ((ds == null) || (ds.Tables[0].Rows.Count <= 0))
            {
                PanelList.InnerHtml = "<table cellspacing='0' cellpadding='0' border='0' style='border-style:none;border-collapse:collapse;width:832px;text-align:center;'><tr><td>[未找到产品设计记录]</td></tr></table>";
            }
            else
            {
                ds.Tables[0].DefaultView.RowFilter = "大类=0";
                for (int i = 0; i < ds.Tables[0].DefaultView.Count; i++)
                {
                    ds.Tables[0].DefaultView[i]["零部件"] = DBNull.Value;
                    ds.Tables[0].DefaultView[i]["外购件"] = DBNull.Value;
                }
                ds.Tables[0].DefaultView.RowFilter = "大类=1";
                for (int i = 0; i < ds.Tables[0].DefaultView.Count; i++)
                {
                    ds.Tables[0].DefaultView[i]["外购件"] = DBNull.Value;
                }
                ds.Tables[0].DefaultView.RowFilter = string.Empty;
                GridCpSj.DataSource = ds;
                GridCpSj.DataBind();
                SetCzQx();
                GridCpSj.SelectedIndex = -1;
                string sCpSjId = Request.QueryString["CpSjId"];
                if ((sCpSjId != null) && (sCpSjId.Trim() != string.Empty))
                {
                    for (int iIndex = 0; iIndex < GridCpSj.DataKeys.Count; iIndex++)
                    {
                        if (GridCpSj.DataKeys[iIndex].Value.ToString() == sCpSjId.Trim())
                        {
                            GridCpSj.SelectedIndex = iIndex;
                            break;
                        }
                    }
                }
            }
        }

        private void SetCzQx()
        {
            if (!IsAccess("CpSjBj.aspx"))
            {
                for (int i = 0; i < GridCpSj.DataKeys.Count; i++)
                {
                    GridCpSj.Rows[i].Cells[10].Controls[3].Visible = false;
                }
            }
            else if (!IsAccess("编辑他人的产品设计"))
            {
                for (int i = 0; i < GridCpSj.DataKeys.Count; i++)
                {
                    if (GridCpSj.DataKeys[i].Values[2].ToString() != (string)Session["RealName"])
                    {
                        GridCpSj.Rows[i].Cells[10].Controls[3].Visible = false;
                    }
                }
            }
        }

        protected void GridCpSj_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CopyNewVersion" && IsAccess("CpSjBj.aspx"))
            {
                int iNewCpSjId = 0;
                if (!ShowErr(BCpSjBj.CopyNewCpSj((int)GridCpSj.DataKeys[Convert.ToInt32(e.CommandArgument)].Value, (string)Session["RealName"], out iNewCpSjId)))
                {
                    Response.Redirect(string.Format("CpSjBj.aspx?CpSjWhere={0}&CpSjId={1}", GridCpSj.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1].ToString().Substring(0, 14), iNewCpSjId));
                }
            }
        }
    }
}